import functionnodes.FunctionNode;
/**
 * @author loaner
 */
class data.Value {
	public static var STRING:Function = String;
	public static var NUMBER:Function = Number;
	public static var XMLNODE:Function = XMLNode;
	public static var FUNCTION:Function = Function;
	public static var FUNCTIONNODE:Function = FunctionNode;
	public static var MOVIECLIP : Function = MovieClip;
	public static var BOOLEAN : Function = Boolean;
	
	public static var ALLVALUETYPES:Array = 
	[
	STRING,
	NUMBER,
	XMLNODE,
	FUNCTION,
	FUNCTIONNODE,
	MOVIECLIP,
	BOOLEAN
	];
	
	public var raw:Function;

	private var _string : String;

	private var _number : Number;
	private var _bool : Boolean;

	public var xml : XMLNode;

	public var type : Function;

	public var func : Function;

	private var funcnode : FunctionNode;

	private var _clip : MovieClip;


	
	public function Value( gottentype:Function, value:Object, xmlEquiv:XMLNode )
	{
		type = gottentype;
		xml = xmlEquiv;
		
		if( type == STRING )
		{
			_string = String( value );
			raw = getString;
		}
		else if( type == NUMBER )
		{
			_number = Number( value );
			raw = getNumber;
		}
		else if( type == XMLNODE )
		{
			xml = XMLNode( value );
			raw = getXMLNode;
		}
		else if( type == FUNCTION )
		{
			func = Function( value );
			raw = getFunction;
		}
		else if( type == FUNCTIONNODE )
		{
			funcnode = FunctionNode( value );
			raw = getFunctionNode;
		}
		else if( type == BOOLEAN )
		{
			_bool = Boolean( value );
			raw = getBoolean;
		}
		else if( type == MOVIECLIP )
		{
			_clip = MovieClip( value );
			raw = getMovieClip;
		}
	}
	
	public function toString()
	{
		return raw().toString();
	}
	
	public function getString():String
	{
		return _string;
	}
	
	public function getNumber():Number
	{
		return _number;
	}
	
	public function getXMLNode():XMLNode
	{
		return xml;
	}
	
	public function getFunction():Function
	{
		return func;
	}
	
	private function getFunctionNode() : FunctionNode 
	{
		return funcnode;
	}

	private function getBoolean() : Boolean 
	{
		return _bool;	
	}

	private function getMovieClip() : MovieClip 
	{
		return _clip;	
	}

	public static function makeTypedValue(arg0 : Object) : Value 
	{
		trace("arg0 is of type " + typeof arg0 );
		
		if( typeof arg0 == "number" )
		{
			return new Value( Value.NUMBER, arg0 );
		}
		else if( typeof arg0 == "string" )
		{
			return new Value( Value.STRING, arg0 );
		}
		else if( typeof arg0 == "boolean" )
		{
			return new Value( Value.BOOLEAN, arg0 );
		}
		else if( typeof arg0 == "function" )
		{
			return new Value( Value.FUNCTION, arg0 );
		}
		else if( typeof arg0 == "movieclip" )
		{
			return new Value( Value.MOVIECLIP, arg0 );
		}
		
		for(var i:Number = 0; i < ALLVALUETYPES.length; i++)
		{
			trace("Going to see if arg0, " + arg0 + ", is of type " + ALLVALUETYPES[i]);
			trace("Is it? " + ( arg0 instanceof ALLVALUETYPES[i] ) );
			if( arg0 instanceof ALLVALUETYPES[i] )
			{
				return new Value( ALLVALUETYPES[i], arg0 );
			}
		}	
		return null;
	}
	
	public function getXML():XMLNode
	{
		var temp:XML;
		var returnxml:XMLNode;
		
		if( type == Value.NUMBER )
		{
			temp = new XML("<symbol>" + raw() + "</symbol>");
		}
		else if( type == Value.STRING )
		{
			temp = new XML("<q>" + raw() + "</q>");
		}
		else if( type == Value.XMLNODE )
		{
			return raw();
		}
		else
		{
			temp = new XML("<symbol>" + raw() + "</symbol>");
		}
		
		
		trace("Value is returning " + temp.firstChild );
		return temp.firstChild;	
	}

}